package com.polaris.live.adapter.level

import android.view.ViewGroup
import android.widget.TextView
import com.bumptech.glide.Glide
import com.hjq.shape.view.ShapeImageView
import com.polaris.live.R
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.ext.dp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.JsonUtils
import com.polaris.live.im.sfs.bean.chat.UpgradeReward
import com.polaris.live.resp.back_resp.RichCharmSizeBean
import com.polaris.live.utils.image.ImageUtils
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder

/**
 * NewGuideBannerAdapter
 *
 * @author Created by 半仙 on 2024/1/17/017 10:06
 */
class LevelUpBannerAdapter : BaseBannerAdapter<UpgradeReward>() {
    override fun bindData(
        holder: BaseViewHolder<UpgradeReward>?,
        data: UpgradeReward?,
        position: Int,
        pageSize: Int,
    ) {
        holder?.apply {
            val image = findViewById<ShapeImageView>(R.id.iv_level_image)
            val title = findViewById<TextView>(R.id.iv_level_title)
            val detail = findViewById<TextView>(R.id.iv_level_detail)
            val tvLook = findViewById<TextView>(R.id.tv_look)
            title.text = data?.title
            detail.text = data?.content
            if (data?.ext != null) {
                val richCharmSize = JsonUtils.fromJson<RichCharmSizeBean>(data.ext)
                if (richCharmSize != null) {
                    val layoutParams = image.layoutParams as ViewGroup.LayoutParams
                    val (showHeight, showWidth) = if (richCharmSize.height != null && richCharmSize.width != null) {
                        (richCharmSize.height.toInt() / 3).autoDp to
                                (richCharmSize.width.toInt() / 3).autoDp
                    } else {
                        100.dp to 100.dp
                    }
                    layoutParams.height = showHeight
                    layoutParams.width = showWidth
                    image.layoutParams = layoutParams
                }
            }
            Glide.with(image).load(ImageUtils.imgPath(data?.photoUrl)).into(image)

            tvLook.setOnSingleClickListener {
                onRouterClick?.invoke(position)
            }
        }
    }

    override fun getLayoutId(viewType: Int): Int {
        return R.layout.adapter_level_up_rich_charm
    }

    private var onRouterClick: ((position: Int) -> Unit)? = null
    fun setOnRouterClick(l: (position: Int) -> Unit) {
        onRouterClick = l
    }

    companion object {
        const val BACK = 1
        const val HONOR = 2
    }
}